<?php
    class Pegawai_model extends Model{
        function Pegawai_model(){
            parent::Model();
        }

        function list_pegawai_header($ro=false) {
            $s = "<table width=\"100%\" border=\"0\" cellpadding=\"2\" cellspacing=\"1\" class=\"tablesorter\">".
                 "<tr><th>No</th><th>Nama</th><th>NIP</th><th>NIP Baru</th><th>Tgl Lahir</th> <!-- <th>JK</th><th>Agama</th> -->".
                 "<th>Jenis</th><th>Pangkat</th><th>Masa Kerja</th><th>Jabatan</th><th>Unit Kerja</th>";
            if ($ro==false) {
                $s .= "<th colspan=3> Aksi </th>";
            }
            $s .= "</tr>";
            return $s;
        }
        
        function list_pegawai_isi($where="", $order="", $limit1=0, $limit2=100) {
            
            if ($where!='') {
                $this->db->where($where);
            }
            
            if ($order == "") {
                $this->db->order_by('nama asc');
            } else {
                $this->db->order_by($order);
            }
            
            
            $this->db->limit($limit2,$limit1);
            $q = $this->db->get('pegawai');

            #$s = "[";
            $s = "";
            $i = $limit1 + 1;
            $order   = array("\r\n", "\n", "\r", "'");
            $replace = ' ';
            set_time_limit(0);
            $this->load->model('Datum_model');
            foreach($q->result() as $row) {
                if ($i % 2 == 1) {
                    $kelas = "event2";
                } else {
                    $kelas = "event";
                }

                $agama = Datum_model::getData('nama', 'agama', array('idspg_agama' => $row->id_agama));
                $jenis_kepegawaian = Datum_model::getData('nama', 'jenis_kepegawaian', array('idspg_jenis_kepegawaian' => $row->id_jenis_kepegawaian));
                $instansi = Datum_model::getData('nama', 'instansi', array('idspg_instansi' => $row->id_instansi));
                $unit_kerja = Datum_model::getData('nama', 'unit_kerja', array('idspg_unit_kerja' => $row->id_unit_kerja));
                
                $id_pangkat_terakhir = Datum_model::getLatestData('idspg_pengangkatan_pangkat', 'pengangkatan_pangkat', array('id_pegawai' => $row->idspg_pegawai));
                $pangkat_terakhir = Datum_model::getData('id_golongan', 'pengangkatan_pangkat', array('idspg_pengangkatan_pangkat' => $id_pangkat_terakhir));
                
                $this->db->select('nama, pangkat');
                $this->db->where(array('idspg_golongan' => $pangkat_terakhir));
                $query = $this->db->get('golongan');
                if ($query->num_rows() > 0) {
                    $r = $query->row();
                    $pangkat = $r->nama.' - '.$r->pangkat;
                } else {
                    $pangkat = '-';
                }

                $id_jabatan_terakhir = Datum_model::getLatestData('idspg_pengangkatan_jabatan', 'pengangkatan_jabatan', array('id_pegawai' => $row->idspg_pegawai));
                $jabatan_terakhir = Datum_model::getData('id_jabatan', 'pengangkatan_jabatan', array('idspg_pengangkatan_jabatan' => $id_jabatan_terakhir));
                $jabatan = Datum_model::getData('nama', 'jabatan', array('idspg_jabatan' => $jabatan_terakhir));                
                $tmt_terakhir = Datum_model::getFirstData('tmt', 'pengangkatan_pangkat', array('id_pegawai' => $row->idspg_pegawai));
                
                if(!empty($row->foto))
                    $nama = "<a id='thumbnail' href='".base_url()."foto_pegawai/$row->foto'>".str_replace($order, $replace, $row->gelar_depan.' '.$row->nama.' '.$row->gelar_belakang)."</a>";
                 else $nama =  str_replace($order, $replace, $row->gelar_depan.' '.$row->nama.' '.$row->gelar_belakang);
                $s .= "<tr class=\"$kelas\" id=\"row-".$row->idspg_pegawai."\" ><td><div align=\"right\">$i.&nbsp;&nbsp;&nbsp;</div></td><td nowrap=\"nowrap\">".$nama."</td>".
                      "<td>".$row->nip."</td><td>".$row->nip_baru."</td><td>".YMD2DMY(trimTime($row->tgl_lahir), TGL_SEPARATOR)."</td>".
                      //"<td>".getJK_LP( $row->jk )."</td><td>".($agama?$agama:"-")."</td>".
                      "<td>".($jenis_kepegawaian?$jenis_kepegawaian:"")."</td><td>".($pangkat?$pangkat:"-")."</td>".
                      "<td>".($tmt_terakhir?getMasaKerja($tmt_terakhir):"-")."</td><td>".($jabatan?$jabatan:"-")."</td>".
                      "<td>".($unit_kerja?$unit_kerja:"-")."</td>";
                
                    $s .= "<td><div align=\"center\"> <a href=\"".base_url()."index.php/pegawai/form_edit/".$row->idspg_pegawai."\" title=\"Ubah Data\" class=\"edit\"><img src=\"".base_url()."images/edit.gif\" border=\"0\"></a></div></td>";
                    if ($this->session->userdata('role') == GROUP_ADMIN) {   
                       $s .=  "<td><div align=\"center\"><a href=\"javascript:deleteRow(".$row->idspg_pegawai.")\"><img src=\"".base_url()."images/del.gif\" border=\"0\"></a></div></td>";
                }
                    if ($this->session->userdata('role') <= GROUP_ADMIN) {   
                       $s .=  "<td><div align=\"center\"><a href=\"#\" onClick=\"window.open('".site_url()."/page/index/".$row->idspg_pegawai."', 'popap', 'width=1200,height=700,scrollbars=yes,location=no');\"><img src=\"".base_url()."images/IconSite.jpg\" border=\"0\"></a></div></td>";
                }
                #$s .= "</tr>'],";
                $s .= "</tr>";

                $i++;
            }
            $s = substr($s, 0, -1);
            #$s .= '];';
            $s .= '</table>';
            return $s;
        }
        
        function list_pegawai($where="", $order="", $ro=false) {
            if ($where!='') {
                $this->db->where($where);
            }
            if ($order == "") {
                $this->db->order_by('nama asc');
            } else {
                $this->db->order_by($order);
            }
                
            $q = $this->db->get('pegawai');

            $s = "
              <table id=\"scrollable\" width=\"100%\" border=\"0\" cellpadding=\"2\" cellspacing=\"1\" class=\"tablesorter\">
                <thead>
                  <tr>
                    <th>No</th>
                    <th>Nama</th>
                    <th>NIP</th>
                    <th>NIP Baru</th>
                    <th>Tgl Lahir</th>
                    <th>JK</th>
                    <th>Agama</th>
                    <th>Jenis Kepegawaian</th>
                    <th>Pangkat</th>
                    <th>Masa Kerja</th>
                    <th>Jabatan</th>
                    <th>Instansi</th>
                    <th>Unit Kerja</th>
                    ";
            if ($ro==false) {
                $s .= " <th colspan='2'> Aksi </th>";
            }
            $s .= "</tr></thead><tbody>";
            $i = 1;

            $this->load->model('Datum_model');
            foreach($q->result() as $row) {
                if ($i % 2 == 1) {
                    $kelas = "event2";
                } else {
                    $kelas = "event";
                }

                $agama = Datum_model::getData('nama', 'agama', array('idspg_agama' => $row->id_agama));
                $jenis_kepegawaian = Datum_model::getData('nama', 'jenis_kepegawaian', array('idspg_jenis_kepegawaian' => $row->id_jenis_kepegawaian));
                $instansi = Datum_model::getData('nama', 'instansi', array('idspg_instansi' => $row->id_instansi));
                $unit_kerja = Datum_model::getData('nama', 'unit_kerja', array('idspg_unit_kerja' => $row->id_unit_kerja));
                
                $pangkat_terakhir = Datum_model::getLatestData('id_golongan', 'pengangkatan_pangkat', array('id_pegawai' => $row->idspg_pegawai));
                $this->db->select('nama, pangkat');
                $this->db->where(array('idspg_golongan' => $pangkat_terakhir));
                $query = $this->db->get('golongan');
                if ($query->num_rows() > 0) {
                    $r = $query->row();
                    $pangkat = $r->nama.' - '.$r->pangkat;
                } else {
                    $pangkat = '-';
                }

                $jabatan_terakhir = Datum_model::getLatestData('id_jabatan', 'pengangkatan_jabatan', array('id_pegawai' => $row->idspg_pegawai));
                $jabatan = Datum_model::getData('nama', 'jabatan', array('idspg_jabatan' => $jabatan_terakhir));
                
                $tmt_terakhir = Datum_model::getLatestData('tmt', 'pengangkatan_pangkat', array('id_pegawai' => $row->idspg_pegawai));
                
                $s .= "<tr class=\"$kelas\" id=\"row-".$row->idspg_pegawai."\" ><td><div align=\"right\">".
                $i."&nbsp;&nbsp;&nbsp;</div></td><td>".$row->gelar_depan.' '.$row->nama.' '.$row->gelar_belakang."</td>".
                "<td>".$row->nip."</td><td>".$row->nip_baru."</td><td>".YMD2DMY(trimTime($row->tgl_lahir), TGL_SEPARATOR)."</td>".
                "<td>".getJK_LP( $row->jk )."</td><td>".($agama?$agama:"-")."</td>".
                "<td>".($jenis_kepegawaian?$jenis_kepegawaian:"")."</td><td>".($pangkat?$pangkat:"-")."</td>".
                "<td>".($tmt_terakhir?getMasaKerja($tmt_terakhir):"")."</td><td>".($jabatan?$jabatan:"-")."</td>
                <td>".($instansi?$instansi:"-")."</td>
                <td>".($unit_kerja?$unit_kerja:"-")."</td>";
                
                if ($ro==false) {
                    $s .= "
                    <td><div align='center'> <a href='".base_url()."index.php/pegawai/form_edit/".$row->idspg_pegawai."' title=\"Ubah Data\" class=\"edit\"><img src='".base_url()."images/edit.gif' border='0'></a></div></td>   
                    <td><div align='center'><a href=\"javascript:deleteRow(".$row->idspg_pegawai.")\"><img src='".base_url()."images/del.gif' border='0'></a></div></td>";
                }
                $s .= "
              </tr>";

                $i++;
            }
            $s .= "</tbody></table>";
            return $s;
        }

        function entry_pegawai($form_id, $form_action, $arr=array()) {
            //format tgl_lahir harus YMD
            if (isset($arr['tgl_lahir'])) {
                $a = explode(TGL_SEPARATOR, trimTime($arr['tgl_lahir']));
                $d = $a[2] * 1;
                $m = $a[1] * 1;
                $y = $a[0];
            } else {
                $d = date("j");
                $m = date("n");
                $y = date("Y");
            }
            
                $foto = (!empty($arr['foto']))?"<img src='".base_url()."foto_pegawai/".$arr['foto']."' width='128px'><br /><center>".anchor('/pegawai/hapus_foto/'.$arr['id'],'[x]hapus')."</center>":'';
            $ret = "
            <form enctype=\"multipart/form-data\" id=\"".$form_id."\" action=\"".$form_action."\" method=\"post\">
            <fieldset >
            <table class=\"formTabel\" border=\"0\" align=\"center\" cellpadding=\"5\" cellspacing=\"1\">
              <tr>
                <td class=\"kolom\" valign=\"top\"></td>
                <td valign=\"top\"></td>
                <td valign=\"top\"></td>
                <td valign=\"top\" rowspan='7'><div style='float:right'> $foto </div></td>
              </tr>
              <tr>
                <td class=\"kolom\" valign=\"top\">Foto</td>
                <td valign=\"top\">:</td>
                <td valign=\"top\"><input type=\"file\" name=\"foto\" id=\"foto\" size=\"22\" /></td>
              </tr>
              <tr>
                <td class=\"kolom\" valign=\"top\">Nama Lengkap</td>
                <td valign=\"top\">:</td>
                <td valign=\"top\"><input type=\"text\" name=\"nama\" id=\"nama\" size=\"38\" value=\"".(isset($arr['nama'])?$arr['nama']:"")."\" /></td>
              </tr>
              <tr>
                <td class=\"kolom\" valign=\"top\">Gelar Depan</td>
                <td valign=\"top\">:</td>
                <td valign=\"top\"><input type=\"text\" name=\"gelar_depan\" id=\"gelar_depan\" value=\"".(isset($arr['gelar_depan'])?$arr['gelar_depan']:"")."\" /></td>
              </tr>
              <tr>
                <td class=\"kolom\" valign=\"top\">Gelar Belakang</td>
                <td valign=\"top\">:</td>
                <td valign=\"top\"><input type=\"text\" name=\"gelar_belakang\" id=\"gelar_belakang\" value=\"".(isset($arr['gelar_belakang'])?$arr['gelar_belakang']:"")."\" /></td>
              </tr>
              <tr>
                <td class=\"kolom\" valign=\"top\">NIP</td>
                <td valign=\"top\">:</td>
                <td valign=\"top\"><input type=\"text\" name=\"nip\" id=\"nip\" value=\"".(isset($arr['nip'])?$arr['nip']:"")."\" /></td>
              </tr>
              <tr>
                <td class=\"kolom\" valign=\"top\">NIP Baru</td>
                <td valign=\"top\">:</td>
                <td valign=\"top\"><input type=\"text\" name=\"nip_baru\" id=\"nip\" value=\"".(isset($arr['nip_baru'])?$arr['nip_baru']:"")."\" /></td>
              </tr>
              <tr>
                <td class=\"kolom\" valign=\"top\">Tempat Lahir</td>
                <td valign=\"top\">:</td>
                <td valign=\"top\"><input type=\"text\" id=\"tempat_lahir\" name=\"tempat_lahir\" size=\"38\" value=\"".(isset($arr['tempat_lahir'])?$arr['tempat_lahir']:"")."\" /></td>
              </tr>
              <tr>
                  <td class='fieldname' valign=\"top\">Tanggal Lahir</td>
                <td valign=\"top\">:</td>
                <td>".combo_tanggal('d',$d,'pilih')."-".
                      combo_bulan('m',$m,'pilih').
                      "-<input name='y' id='y' type='text' size='6' value='$y'/>
                        <a href=\"#\" id=\"date-pick\">Pilih Tanggal</a>       
                </td>
              </tr>
              <tr>
                <td valign=\"top\">Jenis Kelamin</td>
                <td valign=\"top\">:</td>
                <td valign=\"top\">
                  <input type=\"radio\" name=\"jk\" id=\"jk\" value=\"L\" ".(isset($arr['jk'])?(($arr['jk']=='L')?"checked":""):"checked")." />
                  Laki-laki 
                  <input type=\"radio\" name=\"jk\" id=\"jk\" value=\"P\" ".(isset($arr['jk'])?(($arr['jk']=='P')?"checked":""):"")." />
                  Perempuan</td>
              </tr>
              <tr>
                <td valign=\"top\">Status Perkawinan</td>
                <td valign=\"top\">:</td>
                <td valign=\"top\">
                  <input type=\"radio\" name=\"status_perkawinan\" id=\"status_perkawinan\" value=\"1\" ".(isset($arr['status_perkawinan'])?(($arr['status_perkawinan']=='1')?"checked":""):"checked")." />
                  Tidak kawin 
                  <input type=\"radio\" name=\"status_perkawinan\" id=\"status_perkawinan\" value=\"2\" ".(isset($arr['status_perkawinan'])?(($arr['status_perkawinan']=='2')?"checked":""):"")." />
                  Kawin
                  <input type=\"radio\" name=\"status_perkawinan\" id=\"status_perkawinan\" value=\"3\" ".(isset($arr['status_perkawinan'])?(($arr['status_perkawinan']=='3')?"checked":""):"")." />
                  Duda/Janda</td>
              </tr>
              <tr>
                <td class=\"kolom\" valign=\"top\">Nomor Karpeg</td>
                <td valign=\"top\">:</td>
                <td valign=\"top\"><input type=\"text\" name=\"no_karpeg\" id=\"no_karpeg\" size=\"38\" value=\"".(isset($arr['no_karpeg'])?$arr['no_karpeg']:"")."\" /></td>
              </tr>
              <tr>
                <td class=\"kolom\" valign=\"top\">Nomor Karis/Karsu</td>
                <td valign=\"top\">:</td>
                <td valign=\"top\"><input type=\"text\" name=\"no_karis_karsu\" id=\"no_karis_karsu\" size=\"38\" value=\"".(isset($arr['no_karis_karsu'])?$arr['no_karis_karsu']:"")."\" /></td>
              </tr>
              <tr>
                <td class=\"kolom\" valign=\"top\">Agama</td>
                <td valign=\"top\">:</td>
                <td valign=\"top\">".combo_agama('id_agama', (isset($arr['id_agama'])?$arr['id_agama']:""))."</td>
              </tr>
              <tr>
                <td class=\"kolom\" valign=\"top\">Golongan Darah</td>
                <td valign=\"top\">:</td>
                <td valign=\"top\">".combo_golongan_darah('goldar', (isset($arr['goldar'])?$arr['goldar']:""), '-')."</td>
              </tr>
              <tr>
                <td class=\"kolom\" valign=\"top\">Nomor ASKES</td>
                <td valign=\"top\">:</td>
                <td valign=\"top\"><input type=\"text\" name=\"no_askes\" id=\"no_askes\" size=\"38\" value=\"".(isset($arr['no_askes'])?$arr['no_askes']:"")."\" /></td>
              </tr>
              <tr>
                <td class=\"kolom\" valign=\"top\">NPWP</td>
                <td valign=\"top\">:</td>
                <td valign=\"top\"><input type=\"text\" name=\"npwp\" id=\"npwp\" size=\"38\" value=\"".(isset($arr['npwp'])?$arr['npwp']:"")."\" /></td>
              </tr>
              <tr>
                <td class=\"kolom\" valign=\"top\">BAPERTARUM</td>
                <td valign=\"top\">:</td>
                <td valign=\"top\">
                  <input type=\"radio\" name=\"status_bapertarum\" id=\"status_bapertarum\" value=\"1\" ".(isset($arr['status_bapertarum'])?(($arr['status_bapertarum']=='1')?"checked":""):"checked")." />
                  Sudah diambil
                  <input type=\"radio\" name=\"status_bapertarum\" id=\"status_bapertarum\" value=\"2\" ".(isset($arr['status_bapertarum'])?(($arr['status_bapertarum']=='2')?"checked":""):"")." />
                  Belum diambil</td>
              </tr>
              <tr>
                <td class=\"kolom\" valign=\"top\">Jenis Kepegawaian</td>
                <td valign=\"top\">:</td>
                <td valign=\"top\">".combo_jenis_kepegawaian('id_jenis_kepegawaian', (isset($arr['id_jenis_kepegawaian'])?$arr['id_jenis_kepegawaian']:""))."</td>
              </tr>
              <tr>
                <td valign=\"top\">Alamat</td>
                <td valign=\"top\">:</td>
                <td valign=\"top\"><textarea name=\"alamat\" id=\"alamat\" cols=\"45\" rows=\"5\">".(isset($arr['alamat'])?$arr['alamat']:"")."</textarea></td>
              </tr>
              <tr>
                <td class=\"kolom\" valign=\"top\">Instansi Tempat Bekerja</td>
                <td valign=\"top\">:</td>
                <td valign=\"top\">".combo_instansi('id_instansi', (isset($arr['id_instansi'])?$arr['id_instansi']:""))."</td>
              </tr>
              <tr>
                <td class=\"kolom\" valign=\"top\">Unit Kerja</td>
                <td valign=\"top\">:</td>
                <td valign=\"top\">".combo_unit_kerja('id_unit_kerja', (isset($arr['id_unit_kerja'])?$arr['id_unit_kerja']:""))."</td>
              </tr>
              <tr>
                <td valign=\"top\">&nbsp;</td>
                <td valign=\"top\">&nbsp;</td>
                <td valign=\"top\">
                  <input class=\"tombol\" type=\"button\" name=\"btn_kembali\" id=\"btn_kembali\" value=\"<< Kembali\" />
                  <input class=\"tombol\" type=\"submit\" name=\"button\" id=\"button\" value=\"   Simpan   \" />
                </td>
              </tr>
            </table>
            </fieldset>
            </form>
              ";
            
            return $ret;
        }

        function simpan($arr, $is_update=false, $id='') {
               
            if (!$is_update) {
                $this->db->insert('pegawai', $arr);
            } else {
                $this->db->where('idspg_pegawai', $id);
                $this->db->update('pegawai', $arr);
            }
        }
        
    }
?>